﻿using B.S.Outsourcing.Domain;
using B.S.Outsourcing.ErrorCode;
using B.S.Outsourcing.Infatructrue;
using B.S.Outsourcing.Writer.Api.Application.Command;
using MediatR;

namespace B.S.Outsourcing.Writer.Api.Application.CommandHandler
{
    public class DeleteOutSourGetAllCommandHandler : IRequestHandler<DeleteOutSourGetAllCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<OutsourcingModel> baseRepository;
        private readonly DBHelper1 dBHelper;

        public DeleteOutSourGetAllCommandHandler(IBaseRepository<OutsourcingModel> baseRepository, DBHelper1 dBHelper)
        {
            this.baseRepository = baseRepository;
            this.dBHelper = dBHelper;
        }

        public async Task<ApiResult<int>> Handle(DeleteOutSourGetAllCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            const string sql = "UPDATE OutsourcingModel SET IsDelete=1 WHERE Id IN @Ids";
            var parameters = new { Ids = request.Id.Distinct().ToArray() };

            var str = dBHelper.ExecuteNonQuerys(sql,parameters);
            if (str>0)
            {
                result.Code = ApiEnums.成功;
                result.Message = "成功";
                result.Data = str;
            }
            else
            {
                result.Code = ApiEnums.失败;
                result.Message = "失败";
                result.Data = str;
            }
            return await Task.FromResult(result);
        }
    }
}
